package com.kinon.norogamilk.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.kinon.norogamilk.R;
import com.kinon.norogamilk.http.MilkApi;
import com.kinon.norogamilk.http.StrCallback;
import com.kinon.norogamilk.ui.base.BaseActivity;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by lhqq on 2017-12-17.
 * 密码取货
 */

public class PwdTakeActivity extends BaseActivity implements View.OnClickListener {

    private Button btn_back;
    private EditText et_takeCode;
    //    private ImageButton btn_pwdOk;
    private Button btn_takeMilk;
    private TextView tv_pwdhint;
    private boolean isTakeCodeCorrect = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwdtake_layout);
        initView();
    }

    private void initView() {
        btn_back = findViewById(R.id.btn_back);
        et_takeCode = findViewById(R.id.et_takeCode);
//        btn_pwdOk=findViewById(R.id.btn_pwdOk);
        btn_takeMilk = findViewById(R.id.btn_takeMilk);
        tv_pwdhint = findViewById(R.id.tv_pwdhint);

        addOnclick();
    }

    private void addOnclick() {
        btn_back.setOnClickListener(this);
//        btn_pwdOk.setOnClickListener(this);
        btn_takeMilk.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
//            case R.id.btn_pwdOk:
////                getProductByPickup();
//            break;
            case R.id.btn_takeMilk:
                getProductByPickup();
//                if(!isTakeCodeCorrect){
//                    tv_pwdhint.setText("请输入正确的取货码然后点击确认");
//                    return;
//                }
//                Intent intent=new Intent(PwdTakeActivity.this,
//                        PaySuccessActivity.class);
//                startActivity(intent);
//                finish();
                break;
        }
    }

    //取货码取货
    private void getProductByPickup() {
        String takecode = et_takeCode.getText().toString();
        Call<String> call = MilkApi.getInstance()
                .getProductByPickup(sn, takecode);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
//                tv_pwdhint.setText("取货码正确,点击提取进入下一步");
                isTakeCodeCorrect = true;
                machineWs();
            }

            @Override
            public void onError(int code, String error) {
//                tv_pwdhint.setText("取货码错误");
                tv_pwdhint.setText(error);
            }
        });
    }

    private void machineWs() {
        Call<String> call = MilkApi.getInstance().machineWs(sn);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
//                isPauseFind=true;
//                strData=data;
//                visibility();
                Intent intent = new Intent(PwdTakeActivity.this,
                        PaySuccessActivity.class);
                intent.putExtra("isHeat", false);
                intent.putExtra("strTakeData", data);
                startActivity(intent);
                finish();

//                mToast("出货成功");
//                Intent intent=new Intent(MemberTakeActivity.this,PaySuccessActivity.class);
//                startActivity(intent);
//                finish();
            }

            @Override
            public void onError(int code, String error) {
//                mToast(error);
                tv_pwdhint.setText(error);
            }
        });
    }
}
